﻿using System;
using System.Linq;
using System.Windows.Forms;

namespace DriveBeamEditor.Controls.General
{
    public partial class DockGroupBox : GroupBox
    {
        #region Fields

        private Orientation _orientation = Orientation.Horizontal;
        private bool _autoSizeKeyValueControls = true;
        private int _minKvcCaptionSize = 125;

        #endregion

        #region Properties

        public Orientation Orientation
        {
            get { return _orientation; }
            set
            {
                if (_orientation == value) return;
                _orientation = value;
                OnOrientationChanged(new EventArgs());
            }
        }

        public DockStyle ControlDockStyle
        {
            get { return Orientation == Orientation.Horizontal ? DockStyle.Top : DockStyle.Left; }
        }

        public int KeyValueControlsCaptionMinSize
        {
            get { return _minKvcCaptionSize; }
            set
            {
                if (_minKvcCaptionSize == value) return;
                _minKvcCaptionSize = value;
                if(AutoSizeKeyValueControls)
                    kvc_MinCaptionWidthChanged(null, value);
            }
        }

        public bool AutoSizeKeyValueControls
        {
            get { return _autoSizeKeyValueControls; }
            set
            {
                _autoSizeKeyValueControls = value;
                if (value)
                    kvc_MinCaptionWidthChanged(null, KeyValueControlsCaptionMinSize);
            }
        }

        #endregion

        #region CTORs

        public DockGroupBox()
        {
            InitializeComponent();
            Dock = DockStyle.Fill;
        }

        #endregion

        #region Overrides & overridables

        protected override void OnControlAdded(ControlEventArgs e)
        {
            if (e.Control is KeyValueControl)
                ((KeyValueControl)e.Control).MinCaptionWidthChanged += kvc_MinCaptionWidthChanged;

            base.OnControlAdded(e);

            e.Control.Dock = ControlDockStyle;
            PerformDockLayout();
        }

        protected virtual void OnOrientationChanged(EventArgs e)
        {
            PerformDockLayout();
        }

        #endregion

        #region Events

        void kvc_MinCaptionWidthChanged(object sender, int e)
        {
            if (!AutoSizeKeyValueControls) return;

            SuspendLayout();
            foreach (var ctrl in Controls.OfType<KeyValueControl>().Where(ctrl => ctrl != sender))
                ctrl.MinCaptionWidth = e;
            ResumeLayout(true);
        }

        #endregion

        #region Methods

        public void PerformDockLayout()
        {
            SuspendLayout();
            foreach (Control ctrl in Controls)
            {
                ctrl.Dock = ControlDockStyle;
            }
            ResumeLayout(true);
        }

        #endregion
    }
}
